<?php
namespace app\mp\admin;
use app\system\admin\Admin;
use app\common\service\Target as TargetService;

/**
 * 目标管理
 * @author King
 *
 */
class Target extends Admin
{
    protected $hisiModel = 'Target';//模型名称[通用添加、修改专用]
    protected $hisiValidate = 'Target';
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize() {
    	parent::initialize();
    }

    public function edit()
    {
    	$map = ['month_target_count' => '1', 'month_target_money' => '2', 'month_target_cost' => '3', 'month_target_leadtime' => '4', 'year_target_money' => '5'];

    	$targetService = new TargetService();

    	if ($this->request->isPost()) {
    		$post = $this->request->param('post');
    		$curDate = date('Ym');
    		$curYear = date('Y');
    		foreach ($post as $factoryId => $v) {
    			foreach ($v as $sk => $sv) {
    				if (array_key_exists($sk, $map)) {
    					$targetService->saveTarget($factoryId, ($sk=='year_target_money')?$curYear:$curDate, $map[$sk], $sv);
    				}
    			}
    		}

    		return $this->Json(1, '操作成功');
    	}

    	$factoryKV = ['0' => '全部工厂'] + getFactoryKV(true);

    	$assign = [];
    	$assign['list'] = [];
    	foreach ($factoryKV as $factoryId => $factoryName) {
    		$assign['list'][$factoryId] = [
    			'factory_name' => $factoryName,
    			'month_target_count' => $targetService->getTarget($factoryId, 1),
    			'month_target_money' => $targetService->getTarget($factoryId, 2),
    			'month_target_cost' => $targetService->getTarget($factoryId, 3),
    			'month_target_leadtime' => $targetService->getTarget($factoryId, 4),
    			'year_target_money' => $targetService->getTarget($factoryId, 5),
    		];
    	}

    	return $this->assign($assign)->fetch();
    }

}